﻿using System;
using System.Web.UI.WebControls;
using DynamicComparer;
using System.Collections.Generic;
using Shuba.Model;
using System.Data;
using Shuba.Business;
using System.Web;
using System.Configuration;

namespace Shuba.Web.cdc
{
    public partial class cdc_list_parameter : basePage
    {
        string msg = "";

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                InitSort();
                BindData();
            }

            string id = HttpContext.Current.User.Identity.Name;
            if ((id == "88800007") || (id == "00037827") || (id == "88800071") || (id == "88800005") || (id == "88800006") || (id == "00006645"))
            {
                btnReplacement.Visible = true;
            }
            else btnReplacement.Visible = false;
        }

        void BindData()
        {            
            Shuba.Business.CDC _CDC = new Shuba.Business.CDC();
            DataSet list = _CDC.GetListParameter(txtSearch.Text, HttpContext.Current.User.Identity.Name);
            DataSet listAll = _CDC.GetListParameterAll(txtSearch.Text);

            gvList.DataSource = list;
            gvList1.DataSource = listAll;
            gvList.DataBind();
            gvList1.DataBind();

            if (gvList1.Rows.Count == 0)
            {
                ApprovalPnl.Visible = false;
            }

            if (gvList.Rows.Count == 0)
            {
                ApprovalPnl2.Visible = false;
            }
        }

        protected void btnSearch_Click(object sender, EventArgs e)
        {
            BindData();
        }        

        protected void btnSubmit1_Click(object sender, EventArgs e)
        {
            int nSelectedItem = 0;
            foreach (GridViewRow row in gvList1.Rows)
            {
                CheckBox chkItemSelect = (CheckBox)row.FindControl("chkItemSelect");
                string vTypeId = Convert.ToString(gvList1.DataKeys[row.RowIndex].Values["PROSPECT_ID"].ToString());
                if (chkItemSelect.Checked)
                {
                    nSelectedItem++;
                    SaveOperator(vTypeId, true);
                }
            }
            if (nSelectedItem == 0)
            {
                AlertAndRedirect("You Must Select Record to Processed", "#");
                return;
            }
            
            msg = this.GetGlobalResourceObject("Messages", "ApproveAlert").ToString();
            BindData();
            AlertAndRedirect(msg, "cdc_list_parameter.aspx");
            return;          
        }

        private void SaveOperator(string id, bool IsApprove)
        {                                                       
            Shuba.Business.CDC cdc = new Shuba.Business.CDC();            

            if (IsApprove)
            {
                cdc.SaveOperator(HttpContext.Current.User.Identity.Name, id);                
            }
            else
            {                
            }
        }

        protected void btnSubmit_Click(object sender, EventArgs e)
        {
            Shuba.Business.CDC cdc = new Shuba.Business.CDC();

            //cek ada nilai urgent atau ngga.
            for (int i = 0; i < gvList.Rows.Count; i++)
            {
                GridViewRow row = gvList.Rows[i];                
                DropDownList ddlStatus = (DropDownList)row.FindControl("ddlStatus");

                if (ddlStatus.SelectedValue == "2")
                {
                    string id = Convert.ToString(gvList.DataKeys[row.RowIndex].Values["PROSPECT_ID"].ToString());                    
                    cdc.setUrgent(id);
                }
                else
                {
                }
            }

            ValidasiUrutan();
            
            //Response.Redirect("cdc_list_parameter.aspx");  
        }

        private void ValidasiUrutan()
        {
            //cek validasi urutan
            //bool lanjut = true;
            bool cond1 = false;
            bool cond2 = false;

            for (int i = 0; i < gvList.Rows.Count; i++)
            {
                GridViewRow row = gvList.Rows[i];
                DropDownList ddlStatus = (DropDownList)row.FindControl("ddlStatus");

                if ((ddlStatus.SelectedValue == "0")&&(ddlStatus.SelectedValue != "2"))
                {
                    cond1 = true;                    
                }
                if (ddlStatus.SelectedValue == "1" && cond1 == true)
                {
                    cond2 = true;
                }
            }

            if (cond1 && cond2)
            {
                msg = "Invalid process. Please check the data sequence !";
                AlertAndRedirect(msg, "cdc_list_parameter.aspx");
                return;
            }
            else
            {
                Shuba.Business.CDC cdc = new Shuba.Business.CDC();

                for (int i = 0; i < gvList.Rows.Count; i++)
                {
                    GridViewRow row = gvList.Rows[i];
                    DropDownList ddlStatus = (DropDownList)row.FindControl("ddlStatus");

                    if (ddlStatus.SelectedValue == "1")
                    {
                        string id = Convert.ToString(gvList.DataKeys[row.RowIndex].Values["PROSPECT_ID"].ToString());
                        cdc.SetIsBIChecking(id);
                    }
                    else
                    { }
                }

                msg = "Successed !";
                AlertAndRedirect(msg, "cdc_list_parameter.aspx");
                return;
                
            }            

        }

        protected void gvList1_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {

        }

        protected void gvList1_Sorting(object sender, GridViewSortEventArgs e)
        {

        }

        protected void gvList_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {

        }

        protected void gvList_Sorting(object sender, GridViewSortEventArgs e)
        {

        }

        //protected void btnReplace_Click(object sender, EventArgs e)
        //{
        //    Response.Redirect("cdc_replacement_operator.aspx");
        //}        
    }
}